<?php


namespace app\index\controller;

use think\Controller;
use think\facade\Env;

class Xml extends Controller
{
    public function analysis()
    {
        ini_set('max_execution_time', '0');
        // 读取对应文件
        $dirFile = $this->dirFile();
        $fileName = "Home.xsd";
        foreach ($dirFile as $key => $value) {
            if (strpos($value, $fileName)) {
                $file = $value;
                break;
            }
        }
        $attr = $this->xsdArr($file);
        $include = [];//获取该文件加载了哪些其他文件
        if (isset($attr['include']) && count($attr['include'])==1) {//是否加载了文件和加载了几个
            foreach ($attr['include'] as $item) {
                $include[] = $item['schemaLocation'];
            }
        }elseif (isset($attr['include']) && count($attr['include'])>1){//是否加载了文件和加载了几个（大于1是二维数组）
            foreach ($attr['include'] as $item) {
                $include[] = $item['@attributes']['schemaLocation'];
            }
        }
        array_push($include,$fileName);
        //根据不同文件数据结构 判断数据结构(可能是1维数组或者2维数组)
        $element = isset($attr['element'][0])?$attr['element'][0]:$attr['element'];
        //调用公共方法继续处理
        $dom = detailDom($element,$include);
        dump($dom);
        die;

    }

    public function detail()
    {
        // 读取对应文件
        $dirFile = $this->dirFile();
        $fileName = "Gourmet";
        foreach ($dirFile as $key => $value) {
            if (strpos($value, "Product.xsd")) {
                $file = $value;
                break;
            }
        }
        $attr = $this->xsdArr($file);
        $include = ['amzn-base.xsd',$fileName.'.xsd'];//获取该文件加载了哪些其他文件
        //处理加载的哪个xsd文件
        foreach($attr['element']['complexType']['sequence']['element'] as &$type)
        {
            if(isset($type['@attributes']['name']) && $type['@attributes']['name']=='ProductData')
            {
                foreach ($type['complexType']['choice']['element'] as $key=>&$item)
                {
                    if($item['@attributes']['ref']!=$fileName)
                    {
                        unset($type['complexType']['choice']['element'][$key]);
                    }
                }
                $type['complexType']['choice']['element'] = array_values($type['complexType']['choice']['element']);
            }
        }
        //调用公共方法继续处理
        $dom = detailWithDom($attr['element'],$include,$fileName);
        dump($dom);
        die;

    }

    // 把xsd解析成对象或数组
    public function xsdArr($file)
    {
        $xmlFile = str_replace('xsd', 'xml', $file);
        $doc = new \DOMDocument();
        $doc->preserveWhiteSpace = true;
        dump($file);die;
        $doc->load($file);
        $doc->save($xmlFile);
        $myxmlfile = file_get_contents($xmlFile);
        $parseObj = str_replace($doc->lastChild->prefix . ':', "", $myxmlfile);
        $xml_string = simplexml_load_string($parseObj);
        $json = json_encode($xml_string);
        $data = json_decode($json, true);
        return $data;
    }

    //读取全部xsd 路径
    public function dirFile()
    {
        $public = Env::get('root_path') . "public/xsd";
        $dir = getDirContent($public);
        $dirArr = array();
        foreach ($dir as $key => $value) {
            $arr = explode('.', $value);
            if (strtolower($arr[count($arr) - 1]) == "xsd") {
                $dirArr[] = $public . "/" . $value;
            }
        }
        return $dirArr;
    }

}